//
//  BSLWebViewPlugin.m
//  demo
//
//  Created by RYAN on 15/7/28.
//
//

#import "BSLWebViewPlugin.h"
#define NO_FOUND_WEBVIEW @"找不到viewID对应的视图"
#define PARM_ERROR @"PARM_ERROR"
//#define CALLBACKID @"callbackId"
#define SHOWBSLWEBVIEW @"SHOWBSLWEBVIEW"

@implementation BSLWebViewPlugin

- (void)pluginInitialize{
    [super pluginInitialize];
    
    if (![self.viewController isKindOfClass:[BSLWebViewController class]]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWhenBSLWebViewDidDismiss:) name:BSLWebViewDidDismissNotification object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWhenLaunchViewDidLoad:) name:CDVPageDidLoadNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWhenBSLWebViewDidLoad:) name:BSLWebViewDidLoadNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWhenBSLWebViewDidFailLoad:) name:BSLWebViewDidFailLoadNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWhenBSLWebViewLoading:) name:BSLWebViewLoadingNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showWebViewByNot:) name:SHOWBSLWEBVIEW object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWhenBSLWebViewDidRefresh:) name:BSLWebViewDidRefreshNotification object:nil];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWhenBSLWebViewDidShow:) name:BSLWebViewDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWhenBSLWebViewDidHide:) name:BSLWebViewDidHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWhenBSLWebViewDidClose:) name:BSLWebViewDidCloseNotification object:nil];
//    [self.webView.scrollView setScrollsToTop:NO];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BSLWebViewDidDismissNotification object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:CDVPageDidLoadNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BSLWebViewDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BSLWebViewDidHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BSLWebViewDidCloseNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BSLWebViewDidLoadNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BSLWebViewDidFailLoadNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BSLWebViewLoadingNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SHOWBSLWEBVIEW object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BSLWebViewDidRefreshNotification object:nil];
}


#pragma mark - PLUGIN_METHOD
/**
 *  创建并弹出视图
 *
 *  @param command ["url","视图标识","视图styles","动画","动画时间"]
 */
- (void)openWebView:(CDVInvokedUrlCommand *)command{
    CDVCommandStatus status = CDVCommandStatus_OK;
    NSString *message;
    
    NSString *urlString = [command.arguments objectAtIndex:0];
    NSString *viewId = [command.arguments objectAtIndex:1];
    NSDictionary *styles = [command.arguments objectAtIndex:2];
    BSLWebViewController *vc = [self creaetVCWithStyles:styles url:urlString viewID:viewId];
    
    if (!vc) {
        message = @"create view error";
        status = CDVCommandStatus_ERROR;
    }

    else{
        NSString *animation = [command.arguments objectAtIndex:3];
        NSNumber *duraion = [command.arguments objectAtIndex:4];
        self.callbackId = command.callbackId;
        
        [self show:vc animation:animation duration:duraion];
    }
}

/**
 *  创建view但不弹出，存进数组
 *
 *  @param command ["url","视图标识","视图styles"]
 */
- (void)createWebView:(CDVInvokedUrlCommand *)command{

    BOOL isFail;
    NSString *message;
    NSDictionary *viewStyles = [command.arguments objectAtIndex:2];
    
    if (command.arguments.count <3) {
        //TODO WHEN NO PARM
        isFail = YES;
        message = PARM_ERROR;
    }
    
    else{
        NSString *urlString = [command.arguments objectAtIndex:0];
        NSString *viewID = [command.arguments objectAtIndex:1];
        
        BSLWebViewController *controller = [self creaetVCWithStyles:viewStyles url:urlString viewID:viewID];
        
        if (!controller) {
            message = @"create view error";
            isFail = YES;
        }
        else{
            isFail = NO;
            message = @"create view suc";
        }
    }
    
    [self sendMessage:message ById:command.callbackId isFail:isFail];
}

/**
 *  获取应用首页信息
 *
 *  @param command
 */
- (void)getLaunchWebview:(CDVInvokedUrlCommand *)command{
    
    UIWebView *launchView = self.webView;
    NSString *urlString = launchView.request.URL.absoluteString;
    NSString *titleString=[launchView stringByEvaluatingJavaScriptFromString:@"document.title"];
    
    NSDictionary *dic = @{@"url":urlString,@"title":titleString};
    
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dic];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    
}

/**
 *  show已创建过的view
 *
 *  @param command 前端传进来的参数 ["视图标识","附带动画","动画时间"]
 */
- (void)showWebView:(CDVInvokedUrlCommand *)command{
    NSString *viewID = [command.arguments objectAtIndex:0];
    NSString *animation = [command.arguments objectAtIndex:1];
    NSNumber *duration = [command.arguments objectAtIndex:2];
    
    BSLWebViewController *obj = [self getObjectByID:viewID];
    if (obj) {
        self.callbackId = command.callbackId;
        [self show:obj animation:animation duration:duration];
    }
    
    else{
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
    }
}
/**
 *  隐藏一个视图
 *
 *  @param command 前端传进来的参数 ["视图标识","附带动画","动画时间"]
 */
- (void)hideWebView:(CDVInvokedUrlCommand *)command{
    NSString *viewID = [command.arguments objectAtIndex:0];
    NSString *animation = [command.arguments objectAtIndex:1];
    NSNumber *duration = [command.arguments objectAtIndex:2];
    
    BSLWebViewController *obj = [self getObjectByID:viewID];
    if (obj) {
        NSTimeInterval time = [(NSNull *)duration isEqual:[NSNull null]] ? 0.3 : [duration floatValue]*0.001;
        [obj hideView:animation duration:time];
//        [self hide:obj animation:animation duration:duration];
        self.callbackId = command.callbackId;
    }
    
    else{
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
    }
}
/**
 *  关闭一个视图
 *
 *  @param command 前端传进来的参数 ["视图标识","附带动画","动画时间"]
 */
- (void)closeWebView:(CDVInvokedUrlCommand *)command{
    NSString *viewID = [command.arguments objectAtIndex:0];
    NSString *animation = [command.arguments objectAtIndex:1];
    NSNumber *duration = [command.arguments objectAtIndex:2];
    
    self.callbackId = command.callbackId;
    NSTimeInterval time = [(NSNull *)duration isEqual:[NSNull null]] ? 0.3f : [duration floatValue]*0.001;
    BSLWebViewController *obj = [self getObjectByID:viewID];
    
    if (obj) {
        [obj closeViewAnimation:animation duration:time];
    }
    
    else{
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
    }
}

/**
 *  设置下拉刷新
 *
 *  @param command [viewID]
 */
- (void)setPullToRefresh:(CDVInvokedUrlCommand *)command{
    if (command.arguments.count < 2) {
        [self sendMessage:PARM_ERROR ById:command.callbackId isFail:YES];
        return;
    }
    
    NSString *Id = command.arguments[0];
    BSLWebViewController *obj = [self getObjectByID:Id];
    if (!obj) {
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
        return;
    }
    
    NSDictionary *style = command.arguments[1];
    [obj setPullToRefreshWithStyle:style];
    [self sendMessage:@"success" ById:command.callbackId isFail:NO];
}

/**
 *  结束刷新
 *
 *  @param command [viewID]
 */
- (void)endPullToRefresh:(CDVInvokedUrlCommand *)command{
    if (!command.arguments.count) {
        [self sendMessage:PARM_ERROR ById:command.callbackId isFail:YES];
        return;
    }
    
    NSString *Id = command.arguments[0];
    BSLWebViewController *obj = [self getObjectByID:Id];
    if (!obj) {
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
        return;
    }
    
    [obj endPullToRefresh];
    [self sendMessage:@"success" ById:command.callbackId isFail:NO];
}

/**
 *  在view1上添加一个view2
 *
 *  @param command [view1的ID,view2的ID]
 */
- (void)appendWebView:(CDVInvokedUrlCommand *)command{
    if (command.arguments.count<2) {
        [self sendMessage:PARM_ERROR ById:command.callbackId isFail:YES];
        return;
    }
    
    NSString *view1Id = command.arguments[0];
    NSString *view2Id = command.arguments[1];
    
    BSLWebViewController *obj1 = [self getObjectByID:view1Id];
    BSLWebViewController *obj2 = [self getObjectByID:view2Id];
    
    if ([obj2.view superview]) {
        NSString *errorMessage = [NSString stringWithFormat:@"%@视图已被show",view2Id];
        [self sendMessage:errorMessage ById:command.callbackId isFail:YES];
        return;
    }
    
    if (obj1 && obj2) {
        [obj2 removeFromParentViewController];//view2其所有权归view1窗口
        [obj1 addChildViewController:obj2];
        [obj1.webView addSubview:obj2.view];
    }
    
    else{
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
        return;
    }
    
    [self sendMessage:@"success" ById:command.callbackId isFail:NO];
}

/**
 *  在view1上移除view2
 *
 *  @param command [view1的ID,view2的ID]
 */
- (void)removeWebView:(CDVInvokedUrlCommand *)command{
    if (command.arguments.count<2) {
        [self sendMessage:PARM_ERROR ById:command.callbackId isFail:YES];
        return;
    }
    
    NSString *view1Id = command.arguments[0];
    NSString *view2Id = command.arguments[1];
    
    BSLWebViewController *obj1 = [self getObjectByID:view1Id];
    BSLWebViewController *obj2 = [self getObjectByID:view2Id];
    
    if ((obj1 && obj2) && (obj2.view.superview == obj1.webView)) {
        [obj2.view removeFromSuperview];
    }
    
    else{
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
        return;
    }
    
    [self sendMessage:@"success" ById:command.callbackId isFail:NO];
}

/**
 *  加载新URL页面
 *
 *  @param command [viewID,url]
 */
- (void)loadURL:(CDVInvokedUrlCommand *)command{
    if (command.arguments.count < 2) {
        [self sendMessage:PARM_ERROR ById:command.callbackId isFail:YES];
        return;
    }
    
    NSString *viewID = command.arguments[0];
    NSString *urlStr = command.arguments[1];
    
    BSLWebViewController *obj = [self getObjectByID:viewID];
    if (obj) {
        [obj loadUrl:urlStr];
    }
    
    else{
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
        return;
    }
    
    [self sendMessage:@"success" ById:command.callbackId isFail:NO];
}

- (void)back:(CDVInvokedUrlCommand *)command{
    if (command.arguments.count < 1) {
        [self sendMessage:PARM_ERROR ById:command.callbackId isFail:YES];
        return;
    }
    
    NSString *viewID = command.arguments[0];
    BSLWebViewController *obj = [self getObjectByID:viewID];
    if (obj) {
        [obj goBack];
    }
    
    else{
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
        return;
    }
    
    [self sendMessage:@"success" ById:command.callbackId isFail:NO];
}

- (void)canBack:(CDVInvokedUrlCommand *)command{
    if (command.arguments.count < 1) {
        [self sendMessage:PARM_ERROR ById:command.callbackId isFail:YES];
        return;
    }
    
    NSString *viewID = command.arguments[0];
    BSLWebViewController *obj = [self getObjectByID:viewID];
    if (obj) {
        BOOL canBack = [obj canBack];
        [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:canBack] callbackId:command.callbackId];
    }
    
    else{
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
        return;
    }
}

- (void)forward:(CDVInvokedUrlCommand *)command{
    if (command.arguments.count < 1) {
        [self sendMessage:PARM_ERROR ById:command.callbackId isFail:YES];
        return;
    }
    
    NSString *viewID = command.arguments[0];
    BSLWebViewController *obj = [self getObjectByID:viewID];
    if (obj) {
        [obj goForward];
    }
    
    else{
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
        return;
    }
    
    [self sendMessage:@"success" ById:command.callbackId isFail:NO];
}

- (void)canForward:(CDVInvokedUrlCommand *)command{
    if (command.arguments.count < 1) {
        [self sendMessage:PARM_ERROR ById:command.callbackId isFail:YES];
        return;
    }
    
    NSString *viewID = command.arguments[0];
    BSLWebViewController *obj = [self getObjectByID:viewID];
    if (obj) {
        BOOL canBack = [obj canForward];
        [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:canBack] callbackId:command.callbackId];
    }
    
    else{
        [self sendMessage:NO_FOUND_WEBVIEW ById:command.callbackId isFail:YES];
        return;
    }
}

#pragma mark - HELPER

- (void)sendMessage:(NSString *)message ById:(NSString *)callbackId isFail:(BOOL)isFail{
    CDVCommandStatus status = (isFail) ? CDVCommandStatus_ERROR : CDVCommandStatus_OK;
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:status messageAsString:message];
    [self.commandDelegate sendPluginResult:result callbackId:callbackId];
}

- (void)show:(BSLWebViewController *)vc animation:(NSString *)animation duration:(NSNumber *)duration{
    //当子controller的plugin要show一个视图时，发出通知给父controller的plugin进行操作。
    if ([self.viewController isKindOfClass:[BSLWebViewController class]]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:SHOWBSLWEBVIEW object:@{@"viewController":vc,@"animation":animation,@"duration":duration}];
        return;
    }
    
    UIViewController *parentVC = [self parentViewController];
    
    NSTimeInterval time = 0.3;
    if (![(NSNull *)duration isEqual:[NSNull null]]) {
        time = [duration floatValue]*0.001;
    }
    
    if (self.currentVC) {
        //有子视图or子视图隐藏了
        if ([self.currentVC.ID isEqualToString:vc.ID] && !self.currentVC.view.hidden) {
            //子视图即为当前要展示的视图, 且没有hide
            return;
        }
        
        else{
            [vc prepareTransitionWithAnimation:animation duration:duration];
            
            //对应的子视图被hide
            if (self.currentVC.view.hidden && [self.currentVC.ID isEqualToString:vc.ID]) {
                [self.currentVC.view setHidden:NO];
                [vc animating:animation duration:time];
                [[NSNotificationCenter defaultCenter] postNotificationName:BSLWebViewDidShowNotification object:vc];
            }
            
            else{
                [parentVC transitionFromViewController:self.currentVC toViewController:vc duration:time options:UIViewAnimationOptionCurveLinear animations:^{
                    //防止self.currentVC的view过早消失，但这个处理有待优化
                    CGRect frame = [self.currentVC.view frame];
                    frame.origin.x = -0.1;
                    [self.currentVC.view setFrame:frame];
                    
                    [vc animating:animation duration:time];
                    
                } completion:^(BOOL finished) {
                    if (finished) {
                        //TODO
                    }
                }];
            }
        }
    }
    
    else{
        //没有添加任何视图
        [vc prepareTransitionWithAnimation:animation duration:duration];
        [self.viewController.view addSubview:vc.view];
        [vc animating:animation duration:time];
    }
}

/**
 *  判断是否要清空localstorage
 *
 *  @param vc 当前要被添加的视图
 */
- (void)clearLocalStorage:(UIViewController *)vc{
    NSArray *vcs = [[vc parentViewController] childViewControllers];
    int count = 0;
    for (UIViewController *vc in vcs) {
        if ([vc isKindOfClass:[BSLWebViewController class]]) {
            count++;
        }
    }
    
    if (count == 1) {
        [self.webView stringByEvaluatingJavaScriptFromString:@"window.localStorage.setItem(\'allWebViews\',\'[]\')"];
    }
}
/**
 *  获取父视图
 *
 *  @return 父视图控制器
 */
- (UIViewController *)parentViewController{
    if ([self.viewController isKindOfClass:[BSLWebViewController class]]) {
        return [self.viewController parentViewController];
    }
    
    else{
        return self.viewController;
    }
}

//创建视图对象传入数组
- (BSLWebViewController *)creaetVCWithStyles:(NSDictionary *)styles url:(NSString *)url viewID:(NSString *)viewID{
    
    BSLWebViewController *controller = [[BSLWebViewController alloc] init];
    controller.ID = viewID;
    controller.URL = url;
    
    if (controller.webView) {
        
        UIViewController *parentVC = [self parentViewController];
        [parentVC addChildViewController:controller];
        [self clearLocalStorage:controller];
        [controller setStyle:styles];
        
        return controller;
    }
    
    else{
        return nil;
    }
}

- (BSLWebViewController *)getObjectByID:(NSString *)Id{
    
    BSLWebViewController*(^searchChildViewController)(NSArray *childs, NSString *ID) = ^(NSArray *childs, NSString *ID){
        BSLWebViewController *result;
        for (BSLWebViewController *child in childs) {
            if ([child isKindOfClass:[BSLWebViewController class]] && [child.ID isEqualToString:Id]) {
                result = child;
                break;
            }
        }
        return result;
    };
    
    BSLWebViewController *result = nil;
    
    UIViewController *parentVC = [self parentViewController];
    NSArray *childs = [parentVC childViewControllers];
    result = searchChildViewController(childs,Id);
    
    //可能被添加到当前plugin对应的viewcontroller的view上
    if (!result && [self.viewController isKindOfClass:[BSLWebViewController class]]) {
        NSArray *childs = [self.viewController childViewControllers];
        result = searchChildViewController(childs,Id);
    }
    
    return result;
}
/**
 *  删除视图
 *
 *  @param Id 视图标识
 *
 *  @return 返回视图ID
 */
- (BOOL)removeObjectByID:(NSString *)Id{
    
    if (Id.length) {
        BSLWebViewController *controller = [self getObjectByID:Id];
        [controller.view removeFromSuperview];
        [controller removeFromParentViewController];
        return YES;
    }
    
    return NO;
}

#pragma mark - NOTIFICATION_HANDLE

/**
 *  BSLWebView页面开始加载时触发
 *
 *  @param not 通知对象的object指向视图对象
 */
- (void)handleWhenBSLWebViewLoading:(NSNotification *)not{
    
    BSLWebViewController *controller = not.object;
    NSString *js = [JSHelper triggerEvent:controller.ID type:@"loading"];
    [self.webView stringByEvaluatingJavaScriptFromString:js];
}

/**
 *  BSLWebView页面加载错误时触发
 *
 *  @param not 通知对象的object指向视图对象
 */
- (void)handleWhenBSLWebViewDidFailLoad:(NSNotification *)not{
    
    BSLWebViewController *controller = not.object;
    BSLWebView *webView = (BSLWebView *)controller.webView;
    NSString *js = [JSHelper triggerEvent:webView.viewID type:@"error"];
    [self.webView stringByEvaluatingJavaScriptFromString:js];
}

/**
 *  BSLWebView页面加载完成时触发
 *
 *  @param not 通知对象的object指向视图对象
 */
- (void)handleWhenBSLWebViewDidLoad:(NSNotification *)not{
    
    BSLWebViewController *controller = not.object;
    BSLWebView *webView = (BSLWebView *)controller.webView;
    NSString *js = [JSHelper updateWebViewUrl:webView.viewID url:[webView.request.URL absoluteString]];
    [self.webView stringByEvaluatingJavaScriptFromString:js];
}

- (void)handleWhenBSLWebViewDidDismiss:(NSNotification *)not{
    //滑动删除后，传入的是webview的对象
    BSLWebView *webView = not.object;
    NSString *Id = webView.viewID;
    
    if ([self removeObjectByID:Id]) {
        [self.webView stringByEvaluatingJavaScriptFromString:[JSHelper removeWebView:Id]];
    }
}

- (void)handleWhenBSLWebViewDidShow:(NSNotification *)not{
    
    BSLWebViewController *controller = not.object;
    
    if (self.callbackId) {
        [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:self.callbackId];
        self.callbackId = nil;
    }
    //子视图controller不能做更新currentVC的操作，要在父controller中进行
    if (![self.viewController isKindOfClass:[BSLWebViewController class]]) {
        self.currentVC = controller;
        [self.webView stringByEvaluatingJavaScriptFromString:[JSHelper updateCurrentWebviewId:self.currentVC.ID]];
    }
}

- (void)handleWhenBSLWebViewDidHide:(NSNotification *)not{
    
    if (self.callbackId) {
        [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:self.callbackId];
        self.callbackId = nil;
    }
}

- (void)handleWhenBSLWebViewDidClose:(NSNotification *)not{
    
    BSLWebViewController *controller = not.object;

    if (self.callbackId) {
        [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:self.callbackId];
        self.callbackId = nil;
    }
    
    if (![self.viewController isKindOfClass:[BSLWebViewController class]]) {
        if (controller.view.superview) {
            [controller.view removeFromSuperview];
        }
        
        [controller removeFromParentViewController];
        self.currentVC = nil;
        
        NSString *js = [NSString stringWithFormat:@"%@;%@;",[JSHelper updateCurrentWebviewId:@"LaunchWebViewID"],[JSHelper triggerEvent:controller.ID type:@"close"]];
        [self.webView stringByEvaluatingJavaScriptFromString:js];
    }
}

- (void)handleWhenBSLWebViewDidRefresh:(NSNotification *)not{
    if (![self.viewController isKindOfClass:[BSLWebViewController class]]) {
        BSLWebViewController *controller = not.object;
        NSString *js = [JSHelper refreshCallback:controller.ID];
        [self.webView stringByEvaluatingJavaScriptFromString:js];
    }
}

//收到子视图controller的通知，在父controller中进行show的操作
- (void)showWebViewByNot:(NSNotification *)not{
    NSDictionary *dic = not.object;
    [self show:dic[@"viewController"] animation:dic[@"animation"] duration:dic[@"duration"]];
}

@end
